//
//  DispatchGroupAPI.swift
//  GallopFresh
//
//  Created by xiantian on 2023/7/12.
//  Copyright © 2023 鲜恬科技. All rights reserved.
//

import UIKit

class DispatchGroupAPI {
    var dispatchGroup = DispatchGroup()
    var disCount: NSInteger = 0
    var queue: DispatchQueue!
    var callback: (()->Void)?
    
    init() {
        self.dispatchGroup = DispatchGroup()
        self.disCount = 0
    }
    
    func enterGroup() {
        self.disCount = self.disCount + 1
        self.dispatchGroup.enter()
    }
   
    func leaveGroup() {
        if self.disCount == 0 {
            return
        }
        
        if self.disCount > 0 {
            self.disCount = self.disCount - 1
            self.dispatchGroup.leave()
        }
        
        if self.disCount == 0 {
            if self.callback != nil {
                self.dispatchGroup.notify(queue: self.queue, execute: self.callback!)
            }
        }
    }
    
    func notifyGroup(queue: DispatchQueue, execute: @escaping ()->Void) {
        self.queue = queue
        self.callback = execute
    }
}

